package com.nexgo.payment.trans.download;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;

import com.nexgo.payment.R;
import com.nexgo.payment.constrant.TransType;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.fragment.TextDialogFragment;
import com.nexgo.payment.trans.AbsTransActivity;
import com.nexgo.payment.widget.TitleBuilder;
import com.nexgo.sound.SoundManager;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 下载菜单界面
 * 具体包括：回响测试、参数传递、状态上送、下载AID、下载公钥、黑名单下载、
 * 非接参数下载、免密新增BIN表下载、免密卡BIN黑名单下载
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年7月12日
 */
public class DownloadTransActivity extends AbsTransActivity<DownloadTransContract.Presenter>
        implements DownloadTransContract.View {
    private static final String TRANS_ID = "trans_id";
    private static final String SILENT = "silent";
    private static final String TAG = DownloadTransActivity.class.getSimpleName();
    private TransType mTransType;
    private boolean mIsSilent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download_trans);
        ButterKnife.bind(this);
        int transId = getIntent().getIntExtra(TRANS_ID, 0);
        mIsSilent = getIntent().getBooleanExtra(SILENT, false);
        mTransType = TransType.values()[transId];
        new TitleBuilder(this).gone();
        newPresenterInstance();
    }

    //初始化标题栏
    private void initTitle(Activity activity) {
        new TitleBuilder(activity)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(R.drawable.arrow_icon)
                .setTitleText(getString(mTransType.getName()))
                .build();
    }

    private void newPresenterInstance() {
        switch (mTransType) {
            case TRANS_TEST:
                new TransTestPresenter(this, Repository.getInstance());
                break;
            case PARAM_DOWN:
                new TransParamPresenter(this, Repository.getInstance());
                break;
            case UPLOAD_STATE:
                new UploadStatePresenter(this, Repository.getInstance());
                break;
            case AID_DOWN:
                new AidParamPresenter(this, Repository.getInstance());
                break;
            case CAPK_DOWN:
                new CapkParamPresenter(this, Repository.getInstance());
                break;
            case BLACKLIST_DOWN:
                new BlacklistPresenter(this, Repository.getInstance());
                break;
            case RF_PARAM_DOWN:
                new RfParamPresenter(this, Repository.getInstance());
                break;
            case QPS_BIN_B_DOWN:
                new QpsBinBPresenter(this, Repository.getInstance());
                break;
            case QPS_BIN_C_DOWN:
                new QpsBinCPresenter(this, Repository.getInstance());
                break;
            case RESET_GPS:
                new ResetGpsPresenter(this, Repository.getInstance());
                break;
            //xhj add
            case WORK_MASTER_KEY_DOWN:
                new WorkMasterKeyDownPresenter(this, Repository.getInstance());
                break;
            case RISKTRADE:
                new RiskTradePresenter(this, Repository.getInstance());
                break;
            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.start();
    }

    @OnClick({R.id.title_iv_left})
    public void onClick(View view) {
        quitTrans(false);
    }

    public static Intent newInstance(Context mContext, TransType transType) {
        return newInstance(mContext, transType, false);
    }

    public static Intent newInstance(Context mContext, TransType transType, boolean silent) {
        Intent intent = new Intent(mContext, DownloadTransActivity.class);
        intent.putExtra(TRANS_ID, transType.ordinal());
        intent.putExtra(SILENT, silent);
        return intent;
    }
    @Override
    public void showTransSuccess() {
        if (mIsSilent) {
            quitTrans(true);
            return;
        }
        TextDialogFragment dialogFragment = TextDialogFragment.newInstance();
        dialogFragment.setDialogType(TextDialogFragment.DIALOG_TYPE_SUCCESS);
        dialogFragment.setContentText(getString(mTransType.getName()) + getString(R.string.success), Gravity.CENTER);
        dialogFragment.setPositionText(getString(R.string.confirm));
        dialogFragment.setDialogListener(new TextDialogFragment.OnDialogListener() {
            @Override
            public void onCancel() {
            }

            @Override
            public void onConfirm() {
                quitTrans(true);
            }
        });
        dialogFragment.show(getSupportFragmentManager(), null);
    }

    @Override
    public void showTransFailed() {
        if (mIsSilent) {
            quitTrans(false);
            return;
        }
        SoundManager soundManager = new SoundManager();
        soundManager.play(SoundManager.Type.TRANS_FAIL);
        TextDialogFragment dialogFragment = TextDialogFragment.newInstance();
        dialogFragment.setDialogType(TextDialogFragment.DIALOG_TYPE_WRONG);
        dialogFragment.setContentText(getString(mTransType.getName()) + getString(R.string.fail), Gravity.CENTER);
        dialogFragment.setPositionText(getString(R.string.confirm));
        dialogFragment.setDialogListener(new TextDialogFragment.OnDialogListener() {
            @Override
            public void onCancel() {
            }

            @Override
            public void onConfirm() {
                soundManager.stop();
                quitTrans(false);
            }
        });
        dialogFragment.show(getSupportFragmentManager(), null);
    }

    private void quitTrans(boolean isSuccessful) {
        setResult(isSuccessful ? 0 : -1);
        quitTrans();
    }
}
